<template>
	<!-- 我的订单提交订单 -->
	<view>
		<!-- 地址 -->
		<navigator url="/pages/address/address?source=1" class="address-section">
			<view class="order-content">
				<text class="iconfont icon-dizhi"></text>
				<view class="cen">
					<view class="top">
						<text class="name">{{addressData.consignee}}</text>
						<text class="mobile">{{addressData.phone}}</text>
					</view>
					<text class="address">{{addressData.province}} {{addressData.city}} {{addressData.county}} {{addressData.address}}</text>
				</view>
				<text class="iconfont icon-arrow icon" ></text>
			</view>
			<image class="a-bg" src="../../static/gwcymian/ddizi.png" style="width: 40rpx; height: 40rpx;"></image>
		</navigator>

		<view class="goods-section">
			<!-- 商品列表 -->
			<view class="g-item" v-for="(item,index) in arr" :key='index'>
				<image :src='ouk==1 ? item.spuImg : item.img '>
				</image>
				<view class="right">
					<view class="title clamp">{{item.Title}}</view>
					<view class="spec">{{item.title}}</view>
					<view class="price-box">
						<text class="price">￥{{item.originalPrice/100}}</text>
						<text class="number">￥{{item.price/100}}</text>
						<text class="number">*{{item.num}}</text>
					</view>
				</view>
			</view>
		</view>

		<!-- 优惠明细 -->
		<view class="yt-list">
			<view class="yt-list-cell b-b" @click="toggleMask('show')">
				<view class="cell-icon">
					券
				</view>
				<text class="cell-tit clamp">优惠券</text>
				<!-- <image src="../../static/Payment/Alipay.png"></image> -->
				<text class="cell-tip active">
					{{sputitile}}
				</text>
				<text class="cell-more wanjia wanjia-gengduo-d"></text>
			</view>
		</view>
		<!-- 金额明细 -->
		<view class="yt-list">
			<view class="yt-list-cell b-b">
				<text class="cell-tit ">商品金额</text>
				<text class="clamp">￥{{total}}</text>
			</view>
			<view class="yt-list-cell b-b">
				<text class="cell-tit ">折扣金额</text>
				<text class="cell-tip ">-￥{{paid}}</text>
			</view>
			<view class="yt-list-cell b-b" v-if="juan != 0">
				<text class="cell-tit ">优惠卷立减</text>
				<text class="cell-tip ">-￥{{juan/100}}</text>
			</view>
			<view class="yt-list-cell b-b">
				<text class="cell-tit ">运费</text>
				<text class="clamp">免运费</text>
			</view>
			<view class="yt-list-cell desc-cell">
				<text class="cell-tit clamp">备注</text>
				<input class="desc" type="text" v-model="desc" placeholder="请填写备注信息" placeholder-class="placeholder" />
			</view>
		</view>

		<!-- 底部 -->
		<view class="footer">
			<view class="price-content">
				<text>实付款</text>
				<text class="price-tip">￥</text>
				<text class="price">{{shifu}}</text>
			</view>
			<text class="submit" @click="submit">提交订单</text>
		</view>

		<!-- 优惠券面板 -->
		<view class="mask" :class="maskState===0 ? 'none' : maskState===1 ? 'show' : ''" @click="toggleMask">
			<view class="mask-content">
				<!-- 优惠券页面 -->
				<view v-if="arr[0].originalPrice > item.discount" class="coupon-item" v-for="(item,index) in couponList"
					:key="index" @click="toggleMask">
					<view class="con" @click="swichNav(item,index)">
						<view class="left">
							<text class="title">{{item.title}}</text>
							<text class="time">有效期至{{item.gmtEnd}}</text>
						</view>
						<view class="right">
							<text class="price">{{item.discount/100}}</text>
							<text>满{{item.min/100}}可用</text>
						</view>
						<view class="circle l"></view>
						<view class="circle r"></view>
					</view>
					<text class="tips">全品类可用</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				maskState: 0, //优惠券面板显示状态
				desc: '', //备注
				payType: 1, //1微信 2支付宝
				couponList: [],
				addressData: {},
				sputitile: '请选择',
				juan: 0,
				arr: [],
				current: 0,
				order: '',
				gmtEnd: '',
				ouk: null,
				bunol: null,
				kucbz:''
			}
		},
		onLoad(option) {
			console.log(option,"54666666666666666")
			this.init()
			uni.request({
				url: 'http://192.168.1.54:8081/m.api?_gp=address&_mt=getDefAddress ',
				header: {
					'accesstoken': uni.getStorageSync('accessToken')
				},
				success: (res) => {
					// console.log(res,"4655555555")
					this.addressData = res.data.data
				}
			})
			if (option.specSelected != null) {
				this.arr = JSON.parse(option.specSelected);
			} else if (option.data != null) {
				let data = JSON.parse(option.data);
				this.arr = data.goodsData
				let ouk = JSON.parse(option.oprk);
				this.ouk = ouk
			} else if (option.order != null) {
				let that = this
				let id = JSON.parse(option.order)
				that.bunol = JSON.parse(option.omath)
				uni.request({
					url: 'http://192.168.1.54:8081/m.api?_gp=order&_mt=getOrderDetail&orderId=' + id,
					header: {
						'accesstoken': uni.getStorageSync('accessToken')
					},
					success: function(res) {
						// console.a
						that.arr = res.data.data.skuList
						console.log(that.arr)
					}
				})
			}

			//商品数据
		},
		onShow() {
			if (uni.getStorageSync('accessToken') == '') {
				uni.showModal({
					title: '登录提示',
					content: '您尚未登录，是否立即登录',
					success: function(res) {
						if (res.confirm) {
							uni.navigateTo({
								url: '../login/login'
							})
						} else if (res.cancel) {
							// console.log('用户点击取消');
						}
					}
				});
			}
		},
		computed: {
			// 全额
			total() {
				let a = 0
				for (var i = 0; i < this.arr.length; i++) {
					a += this.arr[i].originalPrice * this.arr[i].num
				}
				return a / 100
			},
			// 折扣
			paid() {
				let a = 0
				for (var i = 0; i < this.arr.length; i++) {
					a += (this.arr[i].originalPrice - this.arr[i].price) * this.arr[i].num
				}
				return a / 100
			},
			// 实付
			shifu() {
				let a = 0
				for (var i = 0; i < this.arr.length; i++) {
					a += (this.arr[i].price * this.arr[i].num) - this.juan
				}
				return a / 100
			},
		},
		methods: {
			init() {
				uni.request({
					url: 'http://192.168.1.54:8081/m.api?_gp=coupon&_mt=getUserCoupons ',
					method: 'GET',
					header: {
						'accesstoken': uni.getStorageSync('accessToken')
					},
					success: (res) => {
						// console.log(res,"46555555555555")
						this.couponList = res.data.data
						// console.log(res)
						for (var i = 0; i < res.data.data.length; i++) {//时间戳
							var date = new Date();
							date.setTime(res.data.data[i].gmtEnd);
							var month = date.getMonth() + 1;//月,为什么要加一呢因为跟中国的月份相差1所以有加1
							if (month < 10)
								month = "0" + month;
							var hours = date.getHours();//时
							if (hours < 10)
								hours = "0" + hours;
							var minutes = date.getMinutes();//钟
							if (minutes < 10)
								minutes = "0" + minutes;
											// 年									日
							var time = date.getFullYear() + "-" + month + "-" + date.getDate() + " " + hours + ":" + minutes;
							res.data.data[i].gmtEnd = time
							let that = this
						}
					}
				})
			},
			//显示优惠券面板
			toggleMask(type, index) {
				let timer = type === 'show' ? 10 : 300;
				let state = type === 'show' ? 1 : 0;
				this.maskState = 2;
				setTimeout(() => {
					this.maskState = state;
				}, timer)
			},
			swichNav(item, index) {
				let timeDate = this.couponList[this.current].gmtEnd;
				let Time = new Date(timeDate);
				item.gmtEnd = Time.getTime();
				this.current = index
				this.sputitile = this.couponList[this.current].title
				this.juan = this.couponList[this.current].discount

				console.log(this.couponList, 'wowowow')
			},
			numberChange(data) {
				this.number = data.number;
			},
			changePayType(type) {
				this.payType = type;
			},
			submit() {
				if (this.ouk == 1) {
					// console.log(this.arr, '11111吖')
					let sData = {
						"skuList": this.arr,
						"totalOriginalPrice": this.total * 100, //总原价
						"totalPrice": this.shifu * 100, //总现价 
						"container": this.juan.length == 0 ? [] : [{
							'couponId': this.couponList.couponId,
							'discount': this.couponList.discount,
							'gmtEnd': this.gmtEnd,
							'id': this.couponList.id,
							'min': this.couponList.min,
							'title': this.couponList.title,
							'userId': this.couponList.userId,
						}],
						"mono": "",
						"takeWay": "cart",
						"freightPrice": 0, //地址运费
						"addressId": this.addressData.id, //地址id
					}
					console.log(sData,"745656565656565656565656")
					uni.request({
						url: 'http://192.168.1.54:8081/m.api?_gp=order&_mt=takeOrder',
						data: {
							orderRequest: sData,
							channel: 'ios'
						},
						method: 'GET',
						header: {
							'accesstoken': uni.getStorageSync('accessToken')
						},
						success: res => {
							console.log('1112343454535341', res)
							if (res.data.errmsg == '成功') {
								this.order = res.data.data
								uni.navigateTo({
									url: `/pages/payment/payment?order=${this.shifu}&data=${this.order}`,
								})
							}
						}
					})
				} else {
					// console.log(this.couponList, '11111吖')
					let sData = {
						"skuList": [{
							"skuId": this.arr[0].id,
							"num": this.arr[0].num,
							"title": this.arr[0].Title,
							"originalPrice": this.arr[0].originalPrice,
							"price": this.arr[0].price,
							"vipPrice": this.arr[0].vipPrice,
							"skuTitle": this.arr[0].title,
							"spuImg": this.arr[0].img,
							"stock": this.arr[0].stock,
							"spuId": this.arr[0].spuId,
							"categoryId": this.arr[0].categoryId,
							"categoryIdList": this.arr[0].categoryIds
						}],
						"totalOriginalPrice": this.total * 100, //总原价
						"totalPrice": this.shifu * 100 + this.juan, //总现价 
						"coupon": this.couponList.length == 0 ? [] : {
							'couponId': this.couponList[0].couponId,
							'discount': this.couponList[0].discount,
							'gmtEnd': this.gmtEnd,
							"gmtCreate": this.couponList[0].gmtCreate,
							'id': this.couponList[0].id,
							"gmtUpdate": this.couponList[0].gmtUpdate,
							'min': this.couponList[0].min,
							'title': this.couponList[0].title,//"我来看看"
							'userId': this.couponList[0].userId,
						},
						"mono": "",
						"takeWay": "buy",
						"freightPrice": 0, //地址运费
						"addressId": this.addressData.id, //地址id
					}
					uni.request({
						url: 'http://192.168.1.54:8081/m.api?_gp=order&_mt=takeOrder',
						data: {
							orderRequest: sData,
							channel: 'ios'
						},
						method: 'GET',
						header: {
							'accesstoken': uni.getStorageSync('accessToken')
						},
						success: res => {
							this.kucbz = res.data.errmsg//提示库存不足
							console.log(this.kucbz)
							if (res.data.errmsg == '成功') {
								this.order = res.data.data
								uni.navigateTo({
									url: `/pages/payment/payment?order=${this.shifu}&data=${this.order}`,
								})
							}else{
								uni.showToast({
									title:this.kucbz,
									icon:'none'
								})
							}
						}
					})
				}
			}
		}
	}
</script>

<style>
	.icon {
			width: 20rpx;
			height: 20rpx;
			margin: 37.5rpx;
			border-top: 2px solid #606266;
			border-right: 2px solid #606266;
			transform: rotate(45deg);
		}
page {background: #f8f8f8;padding-bottom: 100upx;}
	.address-section {padding: 30rpx 0;background: #fff;position: relative;}
	.order-content {display: flex;align-items: center;}
	.icon-dizhi {flex-shrink: 0;display: flex;align-items: center;
		width: 90rpx;color: #888;font-size: 44rpx;justify-content: center;
	}
	.cen {display: flex;flex-direction: column;flex: 1;font-size: 28rpx;color: #303133;}
	.name {font-size: 34rpx;margin-right: 24rpx;}
	.address {margin-top: 16rpx;margin-right: 20rpx;color: #909399;padding-left: 30rpx;}
	.icon-you {font-size: 32rpx;color: #909399;margin-right: 30rpx;}
	.a-bg {position: absolute;left: 40rpx;bottom: 0;display: block;width: 100%;height: 5rpx; top: 55rpx;}
	.goods-section {margin-top: 16rpx;background: #fff;padding-bottom: 1px;}
	.g-header {display: flex;align-items: center;height: 84rpx;padding: 0 30rpx;position: relative;}
	.logo {display: block;width: 50rpx;height: 50rpx;border-radius: 100px;}
	.name {font-size: 30rpx;color: #606266;margin-left: 24rpx;}
	.g-item {display: flex;margin: 20rpx 30rpx;}
	.g-item image {flex-shrink: 0;display: block;width: 140rpx;height: 140rpx;border-radius: 4rpx;}
	.g-item .right {flex: 1;padding-left: 24upx;overflow: hidden;}
	.g-item .title {font-size: 30upx;color: #303133;margin-top: 10rpx;}
	.g-item .spec {font-size: 26rpx;color: #909399;padding: 10rpx 0;}
	.g-item .price-box {display: flex;align-items: center;font-size: 32rpx;color: #303133;padding-top: 10rpx;}
	.g-item .price-box .price {margin-bottom: 4rpx;font-size: 24rpx;text-decoration: line-through;}
	.g-item .price-box .number {font-size: 26rpx;color: #606266;margin-left: 20rpx;}
	
	
	.yt-list {margin-top: 16upx;background: #fff;}
	.yt-list-cell {display: flex;align-items: center;padding: 10rpx 30rpx 10rpx 40rpx;line-height: 70rpx;position: relative;}
	.yt-list-cell.cell-hover {background: #fafafa;}
	.yt-list-cell.b-b:after {left: 30rpx;}
	.cell-icon {height: 32rpx;width: 32rpx;font-size: 22rpx;color: #fff;text-align: center;
		line-height: 32rpx;background: #f85e52;border-radius: 4rpx;margin-right: 12rpx;
	}
	.cell-icon.hb {background: #ffaa0e;}
	.cell-icon.lpk {background: #3ab54a;}
	.cell-more {align-self: center;font-size: 24rpx;color: #909399;margin-left: 8rpx;margin-right: -10rpx;}
	.cell-tit {flex: 1;font-size: 26upx;color: #909399;margin-right: 10upx;}
	.cell-tip {font-size: 26rpx;color: #fa436a;}
	.cell-tip.disabled {color: #909399;	}
	.cell-tip.red{color:#fa436a;}
	.yt-list-cell.desc-cell .cell-tit {max-width: 90rpx;}
	.yt-list-cell text{font-size: 24rpx;}
	.desc {flex: 1;font-size: 24rpx;color: #303133;}
	
	
	.footer{position: fixed;left: 0;bottom: 0;z-index: 995;display: flex;align-items: center;
		height: 90rpx;justify-content: space-between;font-size: 30rpx;background-color: #fff;
		color: #606266;box-shadow: 0 -1px 5px rgba(0,0,0,.1);z-index: 998;width: 100%;
	}
	.price-content{padding-left: 30rpx;}
	.price-tip{color: #fa436a;margin-left: 8rpx;}
	.footer .price{font-size: 36rpx;color: #fa436a;}
	.submit{display:flex;align-items:center;justify-content: center;width: 280rpx;
		height: 100%;color: #fff;font-size: 32rpx;background-color: #fa436a;
	}
	/* 优惠券面板 */
	.mask{display: flex;align-items: flex-end;position: fixed;left: 0;top: var(--window-top);bottom: 0;
		width: 100%;background: rgba(0,0,0,0);z-index: 9995;transition: .3s;
	}
	.mask-content{width: 100%;min-height: 30vh;max-height: 70vh;background: #f3f3f3;transform: translateY(100%);transition: .3s;overflow-y:scroll;}
	.mask.none{display: none;}
	.mask.show{background: rgba(0,0,0,.4);}
	.mask.show .mask-content{transform: translateY(0);}
	
	
	/* 优惠券列表 */
	.coupon-item{display: flex;flex-direction: column;margin: 20rpx 24rpx;background: #fff;}
	.coupon-item .con{display: flex;align-items: center;position: relative;height: 120rpx;padding: 0 30rpx;}
	.coupon-item .con:after{position: absolute;left: 0;bottom: 0;content: '';width: 100%;height: 0;
		border-bottom: 1px dashed #f3f3f3;transform: scaleY(50%);
	}
	.coupon-item .left{display: flex;flex-direction: column;justify-content: center;flex: 1;overflow: hidden;height: 100rpx;}
	.coupon-item .title{font-size: 32rpx;color: #303133;margin-bottom: 10rpx;}
	.coupon-item .time{font-size: 24rpx;color: #909399;}
	.coupon-item .right{display: flex;flex-direction: column;justify-content: center;align-items: center;font-size: 26rpx;color: #606266;height: 100rpx;}
	.coupon-item .price{font-size: 44rpx;color: #fa436a;}
	.coupon-item .price:before{content: '￥';font-size: 34rpx;}
	.coupon-item .tips{font-size: 24rpx;color: #909399;line-height: 60rpx;padding-left: 30rpx;}
	.coupon-item .circle{position: absolute;left: -6rpx;bottom: -10rpx;z-index: 10;width: 20rpx;height: 20rpx;background: #f3f3f3;border-radius: 100px;}
	.coupon-item .circle.r{left: auto;right: -6rpx;}
</style>